package Gui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import GameEngine.GameSettings;
import GameEngine.Message;
import GameEngine.Status;

public class GameSettingPanel {

	private JFrame settings;
	private JComboBox<String> iaLevel1,iaLevel2;
	private JRadioButton pvp,pvc,cvc;
	private JCheckBox firstPlayer;
	private ButtonGroup choice;
	private JPanel choiceGameModePan,choiceIaPan,buttonPan;
	private JButton submitSettings,exitSettings;
	private JComboBox<String> iaChoice1,iaChoice2;
	private GameSettings gameSettings;

	private int gameType;
	private String [] gameMode = {"Min/Max", "Random Min/Max", "AlphaBeta", "Random AlphaBeta", "Spin Before Positioning"};
	private String [] iaIntLevel = { "1" , "2" , "3" , "4"};
	private final int MINIMAX = 0;
	private final int ALPHABETA = 1;
	private final int HEURISTIC = 2;
	private final int RANDOMMINMAX = 3;
	private final int RANDOMALPHABETA = 4;

	public GameSettingPanel(){
		gameType=0;
		drawSettingsPanel();
		cvc.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {

				if(cvc.isSelected()){
					iaChoice1.setVisible(true);
					iaLevel1.setVisible(true);
					iaChoice2.setVisible(true);
					iaLevel2.setVisible(true);
					firstPlayer.setVisible(false);
				}

			}

		});

		pvc.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {

				if(pvc.isSelected()){
					firstPlayer.setVisible(true);
					iaChoice1.setVisible(true);
					iaLevel1.setVisible(true);
					iaChoice2.setVisible(false);
					iaLevel2.setVisible(false);
				}

			}

		});

		pvp.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {

				if(pvp.isSelected()){
					iaChoice1.setVisible(false);
					iaChoice2.setVisible(false);
					iaLevel1.setVisible(false);
					iaLevel2.setVisible(false);
					firstPlayer.setVisible(false);
				}

			}

		});

		submitSettings.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				int algType1 = -1;
				int algType2 = -1;
				int levelIA1 = -1;
				int levelIA2 = -1;
				boolean color = false;

				Message newMessage = new Message();


				if(pvp.isSelected()){
					gameType = Status.GAME_PVP;
					color = true;
					newMessage.setGameMode(gameType);
				}else if(pvc.isSelected()){
					gameType = Status.GAME_PVC;
					newMessage.setGameMode(gameType);							
					levelIA1 = iaLevel1.getSelectedIndex() + 1; // la combo box parte da 0
					
					if(iaChoice1.getSelectedIndex() == 0){
						algType1 = MINIMAX;
					}else if (iaChoice1.getSelectedIndex() == 1){
						algType1 = RANDOMMINMAX;
					}else if(iaChoice1.getSelectedIndex() == 2){
						algType1 = ALPHABETA;
					}else if(iaChoice1.getSelectedIndex() == 3){
						algType1 = RANDOMALPHABETA;
					}else{
						algType1 = HEURISTIC;
					}
				}else{
					gameType = Status.GAME_CVC;
					newMessage.setGameMode(gameType);
					levelIA1 = iaLevel1.getSelectedIndex() + 1; // la combo box parte da 0
					levelIA2 = iaLevel2.getSelectedIndex() + 1; // la combo box parte da 0
					
					if(iaChoice1.getSelectedIndex() == 0){
						algType1 = MINIMAX;
					}else if (iaChoice1.getSelectedIndex() == 1){
						algType1 = RANDOMMINMAX;
					}else if(iaChoice1.getSelectedIndex() == 2){
						algType1 = ALPHABETA;
					}else if(iaChoice1.getSelectedIndex() == 3){
						algType1 = RANDOMALPHABETA;
					}else{
						algType1 = HEURISTIC;
					};

					if(iaChoice2.getSelectedIndex() == 0){
						algType2 = MINIMAX;
					}else if (iaChoice2.getSelectedIndex() == 1){
						algType2 = RANDOMMINMAX;
					}else if(iaChoice2.getSelectedIndex() == 2){
						algType2 = ALPHABETA;
					}else if(iaChoice2.getSelectedIndex() == 3){
						algType2 = RANDOMALPHABETA;
					}else{
						algType2 = HEURISTIC;
					}

				}

				if (gameType==Status.GAME_PVC && firstPlayer.isSelected()){
					color=true;
				}

				gameSettings = new GameSettings(gameType,color,algType1,algType2,levelIA1,levelIA2,Status.GAME_ON);


		    	destroySettings();
			}
		});

		exitSettings.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				settings.setVisible(false);
				destroySettings();
				System.exit(0);
			}
			
		});
		
		iaChoice1.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Object selectedItem= iaChoice1.getSelectedItem();
				if(selectedItem.toString().equals("Heuristic")){
					iaLevel1.setVisible(false);
				}else{
					iaLevel1.setVisible(true);
					if(pvc.isSelected()){
						firstPlayer.setVisible(true);
					}
				}
			}
			
		});
		
		iaChoice2.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Object selectedItem= iaChoice2.getSelectedItem();
				if(selectedItem.toString().equals("Heuristic")){
					iaLevel2.setVisible(false);
				}else{
					iaLevel2.setVisible(true);
				}
			}
			
		});
	}

	private void drawSettingsPanel(){
		settings = new JFrame("Game Settings");
		settings.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		settings.setLayout(new GridLayout(3, 0));
		settings.setSize(600, 300);
		settings.setLocationRelativeTo(null);

		choiceGameModePan = new JPanel();
		choiceGameModePan.setLayout(new FlowLayout());
		buttonPan = new JPanel();
		choiceIaPan = new JPanel();
		choice = new ButtonGroup();
		firstPlayer = new JCheckBox("Be First Player");
		firstPlayer.setVisible(false);
		iaLevel1 = new JComboBox<String>(iaIntLevel);
		iaLevel1.setVisible(false);
		iaLevel2 = new JComboBox<String>(iaIntLevel);
		iaLevel2.setVisible(false);
		
		pvp = new JRadioButton("Player vs Player");
		pvp.setSelected(true);
		pvc = new JRadioButton("Player vs IA Player");
		cvc = new JRadioButton("IA Player vs IA Player");

		iaChoice1 = new JComboBox<String>(gameMode);
		iaChoice1.setVisible(false);
		iaChoice2 = new JComboBox<String>(gameMode);
		iaChoice2.setVisible(false);
		submitSettings = new JButton("Submit");
		exitSettings = new JButton ("Cancel");

		choice.add(pvp);
		choice.add(pvc);
		choice.add(cvc);

		choiceGameModePan.add(pvp);
		choiceGameModePan.add(pvc);
		choiceGameModePan.add(cvc);
		choiceIaPan.add(iaChoice1);
		choiceIaPan.add(iaLevel1);
		choiceIaPan.add(iaChoice2);
		choiceIaPan.add(iaLevel2);
		choiceIaPan.add(firstPlayer);

		buttonPan.add(submitSettings);
		buttonPan.add(exitSettings);
		settings.add(choiceGameModePan);
		settings.add(choiceIaPan);
		settings.add(buttonPan);
		settings.setVisible(true);
	}

	private void destroySettings(){
		settings.setVisible(false);
		settings = null;
		choiceGameModePan = null;
		buttonPan = null;
		choice = null;
		choiceIaPan = null;
		iaChoice1 = null;
		iaChoice2 = null;
		pvp = null;
		pvc = null;
		cvc=null;
		firstPlayer=null;
		iaChoice1 = null;
		iaChoice2 = null;
		submitSettings = null;
		exitSettings = null;
	}
	
	public GameSettings getGameSettings() {
		return gameSettings;
	}



}
